<?php

namespace app\service;

use app\model\BookType;
use app\model\BookTypeItem;
use app\model\Member as MemberModel;
use app\model\Post as PostModel;
use app\model\PostRecord as PostRecordService;
use app\model\Item as ItemModel;

class MemberPer
{

    // 角色可以操作的动作
    public $perList = [
        'admin' => ['editor', 'viewer', 'remove'],
        'super_admin' => ['admin', 'editor', 'viewer', 'create', 'update', 'remove'],
        'book_create' => ['admin', 'editor', 'viewer', 'create', 'update', 'remove'],
        'chat_create' => ['admin', 'editor', 'viewer', 'create', 'update', 'remove'],
    ];

    /**
     *  获取用户所拥有权限的数据id
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getALLPerId($param): array
    {
        // 如果是知识库类型,则返回所有知识库权限数据,目前对所有人开放
        if ($param['type'] == 'knowledge') { // 知识库
            $perModel = new BookType();
            $perData = $perModel->select();
            if (!empty($perData)) {
                return $perData->column('id');
            }
        }
        // 如果是知识本类型,则返回知识库下所有知识本的id
        if ($param['type'] == 'book' || $param['type'] == 'chat') { // 知识本,会话
            $perId = $param['book_type_id'];
            // 通过知识库id,查询出所有知识文档id
            $postModel = PostModel::where(['book_type_id' => $perId])->field('id')->select();

            $bookId = [];           // 被赋予的权限文档id
            $knowledgeBookId = [];  // 查询当前用户所有权限下的文档id
            if (!$postModel->isEmpty()) {
                $knowledgeBookId = $postModel->column('id');
            }
            $perModel = MemberModel::where(['type' => $param['type'], 'user_id' => $param['user_id']])->select();
            if (!$perModel->isEmpty()) {
                $bookId = $perModel->column('type_id');
            }
            return array_unique(array_merge($bookId, $knowledgeBookId)); // 返回所有有权限的id
        }
        return [];
    }

    /**
     * 获取属性公开的数据id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPublicPerId(): array
    {
        $bookTypeModel = BookTypeItem::where(['attribute' => ItemModel::PUBLIC_ATTR])->select();
        if (!$bookTypeModel->isEmpty()) {
            return $bookTypeModel->column('item_id');
        }
        return [];
    }


    /**
     * 是否有更改权限
     * @param $nowRole
     * @param $needChangeRole
     * @return bool
     */
    public function isChangePer($nowRole, $needChangeRole): bool
    {
        // 平级没有操作权限
        if ($nowRole == $needChangeRole) {
            return false;
        }
        // 各角色可以操作的权限
        if (!in_array($nowRole, $this->perList[$nowRole])) {
            return false;
        }
        // 不是超级管理员,管理员,没有操作权限
        if (!in_array($nowRole, ['super_admin', 'create', 'create', 'admin'])) {
            return false;
        }
        return true;
    }


    /**
     * 获取文件记录权限
     * @param int $userId
     * @param int $tenantId
     * @param array $typeId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getFileRecordRole(int $userId, int $tenantId, array $typeId): array
    {
        // 被赋予的权限
        $memberModel = MemberModel::where(['type' => 'chat', 'user_id' => $userId, 'type_id' => $typeId, 'tenant_id' => $tenantId])->select();
        if (!$memberModel->isEmpty()) {
            $memberRole = array_column($memberModel->toArray(), null, 'type_id');
        }
        $memberRoleList = [];
        if (!empty($memberRole)) {
            foreach ($memberRole as $k => $v) {
                $memberRoleList[$k] = $v['role'];
            }
        }
        // 创建人
        $postRecordModel = PostRecordService::where(['id' => $typeId, 'user_id' => $userId, 'tenant_id' => $tenantId])->field('id,user_id')->select();
        $postRecordRole = [];
        if (!$postRecordModel->isEmpty()) {
            foreach ($postRecordModel as $v) {
                $postRecordRole[$v['id']] = 'super_admin';
            }
        }
        return $postRecordRole + $memberRoleList;
    }
}
